package controller;

import com.root.health.bean.MedicineInfo;
import com.root.health.bean.UserInfo;
import com.root.health.service.MedicineService;
import com.root.health.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
public class MedicineController {
    @Autowired
    MedicineService medicineService;
    @Autowired
    UserService userService;
    @Autowired
    CorrelationController correlationController;

    /**
     *
     * 查询所有药品信息
     */
    @GetMapping("selectmedinfo")
    public String selectMed(Model model,HttpSession session){
        if(session.getAttribute("loginUser") == null){
            //如果当前用户信息为空，传递message请重新登录
            model.addAttribute("msg","请重新登录");
            //回到登录页面
            return "login";
        }//登录拦截器
        UserInfo tempo_user = (UserInfo) session.getAttribute("loginUser");
        UserInfo userInfo = userService.getUserbyName(tempo_user.getUserName());
        if (userInfo.getRole().equals("parents")) {
            List<MedicineInfo> medList = medicineService.selectMed(userInfo.getUserId());
            model.addAttribute("medList",medList);
            return "selectmed";
        } else {
            List<Map<String,Object>> userMapList = correlationController.ChildrenCorrUser(userInfo);
            session.setAttribute("selectMedFlag",true);
            model.addAttribute("userMapList",userMapList);
            return "selectUserMed";
        }
    }

    /**
     * 子女角色查询药品信息
     * @param userId
     * @param session
     * @param model
     * @return
     */
    @GetMapping("medicineInfoSelectLinked")
    public String selectMedChildrenPage(long userId,HttpSession session,Model model){
        if(session.getAttribute("loginUser") == null){
            //如果当前用户信息为空，传递message请重新登录
            model.addAttribute("msg","请重新登录");
            //回到登录页面
            return "login";
        }//登录拦截器
        UserInfo tempo_user = (UserInfo) session.getAttribute("loginUser");
        UserInfo userInfo = userService.getUserbyName(tempo_user.getUserName());
        session.setAttribute("childrenSelectParentId",userId);
        List<MedicineInfo> medList = medicineService.selectMed(userId);
        model.addAttribute("medList",medList);
        session.removeAttribute("selectMedFlag");
        return "selectmed";
    }
    /**
     *
     * 添加药品信息
     */
    @GetMapping("medicineinfo")
    public String medicinePage(HttpSession session,Model model){
        if(session.getAttribute("loginUser") == null){
            //如果当前用户信息为空，传递message请重新登录
            model.addAttribute("msg","请重新登录");
            //回到登录页面
            return "login";
        }//登录拦截器
        UserInfo tempo_user = (UserInfo) session.getAttribute("loginUser");
        UserInfo userInfo = userService.getUserbyName(tempo_user.getUserName());
        if(userInfo.getRole().equals("children")){
            List<Map<String,Object>> userMapList = correlationController.ChildrenCorrUser(userInfo);
            session.setAttribute("insertMedFlag",true);
            model.addAttribute("userMapList",userMapList);
            return "selectUserMed";
        }
        return "medicineInfo";
    }
    @GetMapping("medicineInfoLinked")
    public String medicineLinked(long userId,HttpSession session,Model model){
        if(session.getAttribute("loginUser") == null){
            //如果当前用户信息为空，传递message请重新登录
            model.addAttribute("msg","请重新登录");
            //回到登录页面
            return "login";
        }//登录拦截器
        session.setAttribute("insertMedParentId",userId);
        session.removeAttribute("insertMedFlag");
        return "medicineInfo";
    }
    @PostMapping("medicineinfo")
    public String medicinePage(MedicineInfo medicineInfo, Model model,HttpSession session){
        session.removeAttribute("insertMedFlag");
        if(session.getAttribute("loginUser") == null){
            //如果当前用户信息为空，传递message请重新登录
            model.addAttribute("msg","请重新登录");
            //回到登录页面
            return "login";
        }//登录拦截器
        UserInfo tempo_user = (UserInfo) session.getAttribute("loginUser");
        UserInfo userInfo = userService.getUserbyName(tempo_user.getUserName());
        if (userInfo.getRole().equals("parents")) {
            MedicineInfo checkmed = medicineService.medByName(medicineInfo.getMedicineName(),userInfo.getUserId());
            if(checkmed != null){
                //判断药品名是否重复
                model.addAttribute("msgformed","药品名重复，请重新填写");
                return "medicineInfo";
            }else{
                medicineService.medicineInfo(
                        medicineInfo.getMedicineName(),medicineInfo.getMedicineEffect(),medicineInfo.getTaboo(),
                        medicineInfo.getIndate(),medicineInfo.getDosage(),medicineInfo.getSpecification(),userInfo.getUserId());
                model.addAttribute("successHref","selectmedinfo");
                return "success";
                //若药品名不重复，则执行添加药品信息方法，将药品信息添加到药品信息数据库
            }
        } else {
            long parentId = (long) session.getAttribute("insertMedParentId");
            MedicineInfo checkmed = medicineService.medByName(medicineInfo.getMedicineName(),parentId);
            if(checkmed != null){
                //判断药品名是否重复
                model.addAttribute("msgformed","药品名重复，请重新填写");
                return "medicineInfo";
            }else{
                medicineService.medicineInfo(
                        medicineInfo.getMedicineName(),medicineInfo.getMedicineEffect(),medicineInfo.getTaboo(),
                        medicineInfo.getIndate(),medicineInfo.getDosage(),medicineInfo.getSpecification(),parentId);
                model.addAttribute("successHref","selectmedinfo");
                return "success";
                //若药品名不重复，则执行添加药品信息方法，将药品信息添加到药品信息数据库
            }
        }
    }
    /**
     *
     * 从药品查询页面携带药品名参数实现删除操作
     */

    @PostMapping("medDel")
    public String medicineLinkDel(){

        return "main";
    }

    @GetMapping("medDel")
    public String medicineLinkDel(long medId,HttpSession session,Model model){
        if(session.getAttribute("loginUser") == null){
            //如果当前用户信息为空，传递message请重新登录
            model.addAttribute("msg","请重新登录");
            //回到登录页面
            return "login";
        }//登录拦截器
        medicineService.deleteMed(medId);
        model.addAttribute("successHref","selectmedinfo");
        return "success";
    }
    //从药品查询页面携带药品名参数实现删除操作
    /**
     *
     * 修改操作中间方法
     */
    @GetMapping("medLinkUpd")
    public String medLinkUpd(long medId,HttpSession session,Model model){
        if(session.getAttribute("loginUser") == null){
            //如果当前用户信息为空，传递message请重新登录
            model.addAttribute("msg","请重新登录");
            //回到登录页面
            return "login";
        }//登录拦截器
        session.setAttribute("updateMedId",medId);
        return "medUpd";
    }
    /**
     *
     * 修改药品信息
     */
    @PostMapping("medUpd")
    public String medUpd(MedicineInfo medicineInfo,HttpSession session,Model model) {
        if (session.getAttribute("loginUser") == null) {
            //如果当前用户信息为空，传递message请重新登录
            model.addAttribute("msg", "请重新登录");
            //回到登录页面
            return "login";
        }//登录拦截器
        long medId = (long) session.getAttribute("updateMedId");
        UserInfo tempo_user = (UserInfo) session.getAttribute("loginUser");
        UserInfo userInfo = userService.getUserbyName(tempo_user.getUserName());
        try {
            MedicineInfo checkmed = medicineService.medByName(medicineInfo.getMedicineName(),userInfo.getUserId());
            if (checkmed != null) {
                //判断药品名是否重复
                model.addAttribute("errormsg", "药品名重复，请重新填写");
                return "error";
            } else {
                medicineService.medUpdate(medicineInfo.getMedicineName(),medicineInfo.getMedicineEffect(),medicineInfo.getTaboo(),
                        medicineInfo.getIndate(),medicineInfo.getDosage(),medicineInfo.getSpecification(),medId);
                model.addAttribute("successHref","selectmedinfo");
                return "success";
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "error";
        }

    }
    @GetMapping("medUpd")
    public String medUpd(){
        return "medUpd";
    }
}
